<?php

namespace App\Http\Controllers;

use App\Exceptions\BusinessException;
use App\Services\OrderService;
use App\Services\WechatService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    /**
     * 美食购物车
     * @param Request $request
     * @return JsonResponse
     */
    public function foodCart(Request $request)
    {
        $cartJson = $request->post('cart_json');
        $data = OrderService::getInstance()->foodCart($cartJson);

        return $this->success($data);
    }

    /**
     * 订单预览
     * @param Request $request
     * @return JsonResponse
     */
    public function orderPreview(Request $request)
    {
        $uid = $request->uid;
        $sellType = $request->post('sell_type');
        $sellId = $request->post('sell_id');
        $preNum = $request->post('pre_num', 1);

        $data = OrderService::getInstance()->orderPreview($uid, $sellType, $sellId, $preNum);

        return $this->success($data);
    }

    /**
     * 下单
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function palaceOrder(Request $request)
    {
        $uid = $request->uid;
        $sellType = $request->post('sell_type');
        $sellId = $request->post('sell_id');
        $preNum = $request->post('pre_num', 1);
        $preDate = $request->post('pre_date');
        $preTime = $request->post('pre_time', '');
        $cartJson = $request->post('cart_json');
        $phone = $request->post('phone', '');
        $data = OrderService::getInstance()->palaceOrder($uid, $sellType, $sellId, $preNum, $preDate, $preTime, $cartJson, $phone);

        return $this->success($data);
    }

    /**
     * 支付
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     * @throws GuzzleException
     */
    public function pay(Request $request)
    {
        $uid = $request->uid;
        $orderId = $request->post('order_id');
        $data = OrderService::getInstance()->pay($uid, $orderId);

        return $this->success($data);
    }

    /**
     * 支付回调
     * @param Request $request
     * @return void
     */
    public function notify(Request $request)
    {
        WechatService::getInstance()->payNotify();
    }

    /**
     * 订单列表
     * @param Request $request
     * @return JsonResponse
     */
    public function orderList(Request $request)
    {
        $uid = $request->uid;
        $page = $request->input('page', 1);
        $pageSize = $request->input('psize', 10);
        $keyword = $request->input('keyword');
        $sellType = $request->input('sell_type');
        $state = $request->input('state');
        $filter = $request->input('filter');
        $data = OrderService::getInstance()->orderList($page, $pageSize, $uid, $keyword, $sellType, $state, $filter);

        return $this->success($data);
    }

    /**
     * 订单详情
     * @param Request $request
     * @return JsonResponse
     */
    public function orderDetail(Request $request)
    {
        $uid = $request->uid;
        $orderId = $request->input('order_id');
        $data = OrderService::getInstance()->orderDetail($uid, $orderId);

        return $this->success($data);
    }

    /**
     * 再来一单
     * @param Request $request
     * @return JsonResponse
     */
    public function orderAgain(Request $request)
    {
        $uid = $request->uid;
        $orderId = $request->post('order_id');
        $data = OrderService::getInstance()->orderAgain($uid, $orderId);

        return $this->success($data);
    }

    /**
     * 删除订单
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function delOrder(Request $request)
    {
        $uid = $request->uid;
        $orderId = $request->post('order_id');
        OrderService::getInstance()->delOrder($uid, $orderId);

        return $this->success();
    }
}
